package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页条件查询护理项目
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {

        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> page = nursingProjectMapper.selectPageByNameAndStatus(name, status);
        return PageResponse.of(page, NursingProjectVo.class);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDto
     * @return
     */
    @Override
    public Integer insert(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setStatus(1);
        int num = nursingProjectMapper.countByNursingProjectName(nursingProject.getName());
        if (num > 0) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL_DUPLTCATE_BED_NUMBER);
        }
        return (nursingProjectMapper.insert(nursingProject));
    }

    /**
     * 根据id删除护理项目
     *
     * @param id 护理项目的唯一标识
     * @return 删除操作的结果，影响的行数
     */
    @Override
    public Integer deleteById(Long id) {
        return (nursingProjectMapper.deleteById(id));
    }

    /**
     * 根据id更新护理项目
     */
    @Override
    public Integer updateNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);
        nursingProject.setUpdateTime(LocalDateTime.now());
        return nursingProjectMapper.update(nursingProject);
    }

    /**
     * 根据id查询护理项目
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo selectNursingProjectById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        if (nursingProject != null) {
            NursingProjectVo nursingProjectVo = BeanUtil.copyProperties(nursingProject, NursingProjectVo.class);
            return nursingProjectVo;
        }
        return null;
    }

    @Override
    public List<NursingProject> getAllNursingProjects() {
        List<NursingProject> nursingProjects = nursingProjectMapper.selectAll();
        if (nursingProjects != null) {
            return nursingProjects;
        }
        return null;
    }


}
